package br.usp.ime.origami.foldcut.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import br.usp.ime.origami.foldcut.gui.canvas.FirstPhaseCanvas;
import br.usp.ime.origami.foldcut.io.Simulator;
import br.usp.ime.origami.foldcut.packing.FirstPhaseManualPacker;
import br.usp.ime.origami.foldcut.packing.Packer;
import br.usp.ime.origami.foldcut.structures.simplegraph.Graph;

public class Screen implements ActionListener
{

	private JButton next = new JButton(">");

	private JButton previous = new JButton("<");

	private JButton fastForward = new JButton(">>");

	private JButton rewind = new JButton("<<");

	private JButton run = new JButton("run");

	private JProgressBar progressBar = new JProgressBar();

	private JFrame frame;

	private JPanel controls = new JPanel();

	private JPanel main = new JPanel();

	private Simulator simulator;

	private FirstPhaseManualPacker packer;

	public Screen(FirstPhaseManualPacker packer)
	{
		this.packer = packer;
		frame = new JFrame("Fold and Cut Problem with Disk-Packing");
		frame.setLayout(new BorderLayout());

		controls.setLayout(new FlowLayout());
		controls.add(rewind);
		controls.add(previous);
		controls.add(progressBar);
		controls.add(next);
		controls.add(fastForward);

		for (Step s : Step.values())
		{
			JButton button = new JButton(s.getName());
			button.setEnabled(false);
			controls.add(button);
		}

		controls.add(run);

		rewind.addActionListener(this);
		previous.addActionListener(this);
		next.addActionListener(this);
		fastForward.addActionListener(this);
		run.addActionListener(this);

		next.setEnabled(false);
		previous.setEnabled(false);
		fastForward.setEnabled(false);
		rewind.setEnabled(false);

		progressBar.setString("10/30");

		open();
	}

	public void execute(Simulator simulator)
	{
		this.simulator = simulator;

		for (Component component : this.controls.getComponents())
		{
			if (component instanceof JButton)
			{
				JButton button = (JButton) component;
				button.setEnabled(true);
			}
		}
		run.setEnabled(false);
		progressBar.setMaximum(simulator.getStepCount());

	}

	public void open()
	{
		frame.add(controls, BorderLayout.NORTH);
		controls.setBackground(Color.WHITE);
		frame.add(main, BorderLayout.CENTER);
		main.setBackground(Color.WHITE);
		frame.setSize(800, 700);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

	public void actionPerformed(ActionEvent e)
	{
		JButton button = (JButton) e.getSource();

		if (button == next)
		{
			this.simulator.next(1);
		}
		else if (button == previous)
		{
			this.simulator.previous(1);
		}
		else if (button == fastForward)
		{
			this.simulator.next(10);
		}
		else if (button == rewind)
		{
			this.simulator.previous(10);
		}
		else if (button == run)
		{
			Graph g = packer.calculateGraph();
			Packer p = new Packer(g, packer.getDisks());
			p.execute();
			execute(Simulator.getInstance());
			return;
		}
		else
		{
			Step step = Step.valueOf(button.getText());
		}

		if (this.main.getComponentCount() > 0)
			this.main.remove(0);
		this.main.add(new JLabel(new ImageIcon(this.simulator.getBufferedImageActual())));

		// progressBar.setValue(this.simulator.getStep());
		// System.out.println(progressBar.getValue());
		// progressBar.repaint();

		controls.repaint();
		frame.setVisible(true);

	}

	public void setCanvas(FirstPhaseCanvas canvas)
	{
		this.main.add(canvas);
		this.frame.setVisible(true);
	}

}
